package platform.persistence.config;

import com.mongodb.MongoClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.net.UnknownHostException;

/**
 * Mongo Config
 *
 * @author roamer
 * @version V1.0
 * @date 2019-02-17 16:11
 */
@Configuration
@PropertySource("classpath:db/mongo.properties")
public class MongoConfigContext {

    @Value("${mongo.host}")
    private String mongoHost;

    @Value("${mongo.port}")
    private int mongoPort;

    @Value("${mongo.database}")
    private String mongoDatabase;

    @Bean
    public MongoDbFactory mongoDbFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(new MongoClient(mongoHost, mongoPort), mongoDatabase);
    }

    @Bean
    public StanderMongoTemplate mongoTemplate() throws UnknownHostException {
        return new StanderMongoTemplate(mongoDbFactory());
    }
}
